package cn.itrip.beans.controller;

import cn.itrip.beans.dtos.Dto;
import cn.itrip.search.service.SearchService;
import cn.itrip.search.vo.ItripHoteVo;
import cn.itrip.common.DtoUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/hotellist")
public class SearchController{
    @Resource
    private SearchService searchService;
    @PostMapping("/searchItripHotelListByHotCity")
    public Dto searchItripHotelListByHotCity(@RequestBody Map param){
        try {
            List<ItripHoteVo> list = searchService.searchItripHotelListByHotCity(param.get("cityId") ,(Integer)param.get("count"));
            return DtoUtils.returnDataSuccess(list);
        } catch (Exception e) {
            return DtoUtils.returnFail(e.getMessage(),"50001");
        }
    }
    public static  void main(String[] args) throws Exception{
        String url = "http://localhost:9080/solr/hotel";
        HttpSolrClient client = new HttpSolrClient(url);
        client.setParser(new XMLResponseParser());
        client.setConnectionTimeout(500);
        SolrQuery query = new SolrQuery();
        query.setQuery("keyword:北京");
        List<ItripHoteVo> list = client.query(query).getBeans(ItripHoteVo.class);
        list.forEach(vo ->{
            System.out.println(vo.getHotelName()+"---"+vo.getAddress());
        });
    }
}
